package org.lg.studyspringboot20.controller;


import org.lg.studyspringboot20.dao.MongodbDao;
import org.lg.studyspringboot20.dto.DTO;
import org.lg.studyspringboot20.entity.Admin;
import org.lg.studyspringboot20.entity.MongoDBDocument;
import org.lg.studyspringboot20.entity.Permission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;
import java.util.Arrays;
import java.util.List;

@Controller //@Controller @Service @Repository @Component
@RequestMapping("/admin1Controller")
public class Admin1Controller {
    @Autowired
    MongodbDao mongodbDao;

    @RequestMapping("/add")
    @ResponseBody
    public DTO add1(){
        Admin admin = new Admin("0001","aaaa","123",0,"");
        List<Permission> permissions = Arrays.asList(new Permission[]{new Permission("0001", "查看用户"), new Permission("0002", "删除用户")});
        admin.setPermissions(permissions);
        System.out.println("添加到mysql数据库");
        MongoDBDocument<Admin> mongoDBDocument = new MongoDBDocument<Admin>("admin",admin);//文档名称（类似表名 和 当前对象（相当于一行））
        //存mongodb数据库
        mongodbDao.insertWithKey(mongoDBDocument,admin.getAdid());
        return new DTO(200,"添加管理员1成功");
    }

    @RequestMapping("/selectByPK/{adid}")
    @ResponseBody
    public DTO selectByPK(@PathVariable("adid")String adid){ //从路径中的指定位置绑定值

        System.out.println("查询mongodb数据库");

        MongoDBDocument document = mongodbDao.findByKey("admin", adid, Admin.class);
        return new DTO(200,"添加管理员1成功",document.getT());
    }
}
